# -*- coding: utf-8 -*-
__author__ = 'xujianhua'
#atp的实现进程
import multiprocessing
import threading
from lcopa import load_uphi_data
from atp import atp
import cv2
class ATPPorcess:
    def __init__(self,img_queue):
        self.img_queue = img_queue

        # self.thread = threading.Thread(target=self.__wait_process)
        self.run = False
        self.x={"lcopa":"","uphi":""}
        self.y={"lcopa":"","uphi":""}
        self.use_x = False
        self.use_y = False
    def setting(self,data):
        print data
        try:
            for v in data["setData"]:
                if (v["type"] == 0):
                    self.x = {"lcopa": v["lcopa"], "uphi": (load_uphi_data.u_phi("./u-phi/"+v["u-phi"])).U_PHI}
                    self.use_x = True
                else:
                    self.y = {"lcopa": v["lcopa"], "uphi": (load_uphi_data.u_phi("./u-phi/"+v["u-phi"])).U_PHI}
                    self.use_y = True
        except e:
            print(e)
            return 202
        return 200
    def start(self):
        if(self.use_x|self.use_y):
            self.run = True
            self.atp_process = multiprocessing.Process(target=self.__atp)
            self.atp_process.start()
            return 200
        else:
            return 204
        # self.thread.start()
        # self.atp_process.start()
    def stop(self):
        if (self.use_x | self.use_y):
            try:
                self.run = False
                self.atp_process.terminate()
            except e:
               print e
               return 205
            return 200
        else:
            return 204
    def __wait_process(self):
        while(self.run):
            pass
            # cmd = self.queue.get()
            # if(cmd["opType"] == )
            # # for data in  data["setData"]:
                # load_uphi_data.u_phi(data["u-phi"])


    def __atp(self):
        count = 0
        cap = cv2.VideoCapture(0)
        cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH,640)
        cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT,320)
        cap.set(cv2.cv.CV_CAP_PROP_FPS,30)
        rate = 20

        print "rate:",rate
        cal_frame = 0
        frame_err = 0.0
        toal_angle = 0.0
        MAX_FRAME = 10
        last_ave = 0
        # lcopausb = lcopa_usb.LcopaUsb()
        # lcopausb.open(0x1234)
        # mu_phi = load_uphi_data.u_phi("./u-phi/DF2015-FA02.txt")
        # print mu_phi.toString()
        # lcoap = calculate_codev.lcopa_vol(mu_phi)
        # btyes=lcoap.calculate_voltage((0.003))
        # lcopausb.send_byte(btyes)
        # bp = bp_nn.BPNN('','')
        # str_time = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
        # fobj=open("./"+str_time+".txt",'w')
        while(True):
            # Capture frame-by-frame
            ret, frame = cap.read()
            count = count + 1
            self.img_queue.put(frame)
            # font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SCRIPT_SIMPLEX, 1, 1, 0, 3, 8)
            cv2.putText(frame, str(rate), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0))
            cv2.putText(frame, str(count), (10, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0))
            # cv2.putText(frame,"10",(10,10),font,cv2.FONT_HERSHEY_COMPLEX)
            cv2.imshow("frame",frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                # fobj.flush()
                # fobj.close()

                break
        cv2.destroyAllWindows()